2月 26 2015 Online Judge►UVa [UVa] 100 - The 3n + 1 problem 重新再寫過一次這題,時間從0.955進步到0.075,差別在於做過的記錄下來,有想到區間似乎也可以記錄下來?但用二維記憶體就爆炸了。 而那些0.000的是怎麼一回事.. 12345678910111213141516171819202122232425262728293031323334353637383940414243#include <iostream>#include <vector>using namespace std;int main(){ vector <int> v ; v.resize(1000000) ; for ( int i = 0 ; i < 1000000 ; i ++ ) v[i] = 0 ; for ( int n , m ; cin >> n >> m ; ) { cout << n << " " << m ; if ( n > m ) { int temp = n ; n = m ; m = temp ; } int maxlength = 0 ; for ( int i = n ; i <= m ; i ++ ) { if ( v[i] != 0 ) maxlength = max(maxlength,v[i]); else { int j = i , count = 0 ; while ( j > 1 ) { if ( j % 2 == 0 ) j /= 2 ; else j = 3 * j + 1 ; count ++ ; } v[i] = count ; maxlength = max(maxlength,v[i]); } } cout << " " << maxlength + 1 << endl ; } return 0;} Newer [UVa] 10583 - Ubiquitous Religions Older [UVa] 10165 - Stone Game